1
Pengantar PyTorch: Mengapa Tensor Penting
EvoClass-AI002Lecture 1
00:00

Pengantar PyTorch: Mengapa Tensor Penting

PyTorch adalah kerangka kerja sumber terbuka yang sangat fleksibel dan dinamis, digemari untuk penelitian pembelajaran mendalam dan prototipe cepat. Di intinya, Tensor adalah struktur data yang tak tergantikan. Ini adalah larik multi-dimensi yang dirancang untuk menangani operasi numerik secara efisien yang dibutuhkan oleh model pembelajaran mendalam, mendukung akselerasi GPU secara otomatis. akselerasi GPU automatically.

1. Memahami Struktur Tensor

Setiap input, output, dan parameter model dalam PyTorch dikemas dalam bentuk Tensor. Mereka memiliki fungsi yang sama seperti array NumPy tetapi dioptimalkan untuk pemrosesan pada perangkat keras khusus seperti GPU, membuatnya jauh lebih efisien untuk operasi aljabar linear skala besar yang dibutuhkan oleh jaringan saraf.

Properti utama yang mendefinisikan tensor:

  • Bentuk: Menentukan dimensi data, dinyatakan sebagai tuple (misalnya, $4 \times 32 \times 32$ untuk satu kelompok gambar).
  • Tipe Data: Menentukan tipe numerik dari elemen yang disimpan (misalnya, torch.float32 untuk bobot model, torch.int64 untuk pengindeksan).
  • Perangkat: Menunjukkan lokasi perangkat keras fisik: biasanya 'cpu' atau 'cuda' (GPU NVIDIA).
Grafik Dinamis dan Autograd
PyTorch menggunakan model eksekusi imperatif, artinya grafik komputasi dibangun saat operasi dieksekusi. Hal ini memungkinkan mesin diferensiasi otomatis bawaan, Autograd, untuk melacak setiap operasi pada Tensor, selama properti requires_grad=True diatur, memungkinkan perhitungan gradien yang mudah saat melakukan backpropagation.
fundamentals.py
TERMINALbash — pytorch-env
> Ready. Click "Run" to execute.
>
TENSOR INSPECTOR Live

Run code to inspect active tensors
Question 1
Which command creates a $5 \times 5$ tensor containing random numbers following a uniform distribution between 0 and 1?
torch.rand(5, 5)
torch.random(5, 5)
torch.uniform(5, 5)
torch.randn(5, 5)
Question 2
If tensor $A$ is on the CPU, and tensor $B$ is on the CUDA device, what happens if you try to compute $A + B$?
An error occurs because operations require tensors on the same device.
PyTorch automatically moves $A$ to the CUDA device and proceeds.
The operation is performed on the CPU, and the result is returned to the CPU.
Question 3
What is the most common data type (dtype) used for model weights and intermediate calculations in Deep Learning?
torch.float32 (single-precision floating point)
torch.int64 (long integer)
torch.bool
torch.float64 (double-precision floating point)
Challenge: Tensor Manipulation and Shape
Prepare a tensor for a specific matrix operation.
You have a feature vector $F$ of shape $(10,)$. You need to multiply it by a weight matrix $W$ of shape $(10, 5)$. For matrix multiplication (MatMul) to work, $F$ must be 2-dimensional.
Step 1
What should the shape of $F$ be before multiplication with $W$?
Solution:
The inner dimensions must match, so $F$ must be $(1, 10)$. Then $(1, 10) @ (10, 5) \rightarrow (1, 5)$.
Code: F_new = F.unsqueeze(0) or F_new = F.view(1, -1)
Step 2
Perform the matrix multiplication between $F_{new}$ and $W$ (shape $(10, 5)$).
Solution:
The operation is straightforward MatMul.
Code: output = F_new @ W or output = torch.matmul(F_new, W)
Step 3
Which method explicitly returns a tensor with the specified dimensions, allowing you to flatten the tensor back to $(50,)$? (Assume $F$ was $(5, 10)$ initially and is now flattened.)
Solution:
Use the view or reshape methods. The fastest way to flatten is often using -1 for one dimension.
Code: F_flat = F.view(-1) or F_flat = F.reshape(50)